package com.search.analysis.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.search.FxApplication;
import com.search.analysis.Analysis;
import com.search.entity.SearchContentEntity;
import com.search.utils.JsonFastJsonUtil;
import com.search.utils.MapAndBean;
import com.search.utils.OkHttpUtils;
import javafx.concurrent.Worker;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 百度
 * https://www.baidu.com/s?ie=UTF-8&wd=java%20gui&tn=json&rn=10&pn=0&lm=360
 * ● wd  关键词
 * ● rn  一页显示多少条信息
 * ● pn 翻页   从0 开始
 * ● lm 搜索时间  值为0、1、7、30、360，以天为单位
 * ● ie  utf-8    页面编码
 * ● tn  显示模式   tn=baidulocal (无广告模式)   ,  tn=json
 */
@Component
public class BaiduAnalysis implements Analysis {
    @Override
    public String getData(String keyWord) {

        String url = "http://www.baidu.com/s?ie=UTF-8&wd=" + keyWord + "&rn=20&pn=0&lm=360&tn=json";
        String html=null;
        while (true) {
          html =  OkHttpUtils.builder("baidu").
                    url(url).
                    get().
                    sync();
            boolean contains = html.contains("<title>百度安全验证</title>");
            if (!contains) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return html;
    }

    @Override
    public List<Map<String, Object>> extract(String json) {
        JSONObject parse = JsonFastJsonUtil.parse(json, JSONObject.class);
        JSONObject feed = (JSONObject) parse.get("feed");
        JSONArray entrys = (JSONArray) feed.get("entry");
        List<Map<String, String>> maps = JsonFastJsonUtil.parse(entrys.toString(), List.class);
        List<Map<String, Object>> list = maps.stream().parallel().map(map -> {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("url", map.get("url"));
            map1.put("abs", map.get("abs"));
            map1.put("title", map.get("title"));
            map1.put("time", map.get("time"));
            return map1;
        }).collect(Collectors.toList());
        System.out.println("百度查询到的数量:"+list.size());
        return list;
    }

    @Override
    public List<SearchContentEntity> rinse(List<Map<String, Object>> list) {
        List<SearchContentEntity> searchContentEntities = new CopyOnWriteArrayList<>();
        list.stream().parallel().forEach(stringStringMap -> {
            if (stringStringMap.get("url") == null||stringStringMap.get("title") == null) {
                return;
            }
            SearchContentEntity searchContentEntity = MapAndBean.mapToDomain(stringStringMap, SearchContentEntity.class);
            searchContentEntity.setContent(stringStringMap.get("abs")==null?"":stringStringMap.get("abs").toString());
            searchContentEntities.add(searchContentEntity);
        });
        return searchContentEntities;
    }

    @Override
    public List<SearchContentEntity> process(List<SearchContentEntity> list) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<SearchContentEntity> collect = list.stream().parallel().map(searchContentEntity -> {
            searchContentEntity.setAuthor("百度");
            searchContentEntity.setSource("百度");
            Date date = new Date(Long.parseLong(searchContentEntity.getTime()) * 1000);
            searchContentEntity.setTime(simpleDateFormat.format(date));
            return searchContentEntity;
        }).collect(Collectors.toList());
        return collect;
    }

}
